package com.manager.common.tcp.client;

import com.manager.common.tcp.decode.impl.BusinessDecode;
import com.manager.common.tcp.watch.TcpClientWatchDog;
import com.manager.common.utils.SocketByteConvertUtils;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.util.concurrent.LinkedBlockingQueue;

public class TcpClient {

    private ChannelFuture future;

    private static LinkedBlockingQueue<BusinessDecode> receiptQueue = new LinkedBlockingQueue<BusinessDecode>();

    private static LinkedBlockingQueue<BusinessDecode> disposeReportCommandQueue = new LinkedBlockingQueue<BusinessDecode>();

    private TcpClient() {

    }

    private static TcpClient instance = new TcpClient();

    public static TcpClient getInstance() {
        return instance;
    }

    public void connect(String ip, int port) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                EventLoopGroup workerGroup = new NioEventLoopGroup();
                try {
                    /**客户端相关配置信息*/
                    Bootstrap bootstrap = new Bootstrap();
                    bootstrap.group(workerGroup);
                    bootstrap.channel(NioSocketChannel.class);
                    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
                    bootstrap.handler(new TcpClientChannel());

                    future = bootstrap.connect(ip, port).sync();
                    future.channel().closeFuture().sync();

                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    workerGroup.shutdownGracefully();
                }
            }
        }).start();
    }

    public static LinkedBlockingQueue<BusinessDecode> getReceiptQueue() {
        return receiptQueue;
    }

    public void send(byte[] data) {
        future.channel().writeAndFlush(data);
    }

    public  LinkedBlockingQueue<BusinessDecode> getDisposeReportCommandQueue() {
        return disposeReportCommandQueue;
    }

    public static void main(String[] args) throws Exception {
        /***
         * {
         * “serialNumber”: ‘123456789’,
         * “type”:1,
         * “filepath”: ‘/pa5h/th/xx.pcap’ //读完就删
         * }
         * */
       /* TcpClient tcpClient = new TcpClient();

        new Thread(()->{
            try{
                Thread.sleep(5000);
                JSONObject params = new JSONObject();
                params.put("serialNumber",System.currentTimeMillis());
                params.put("type", MimicryTcpFileType.STATE_COUNT.getCode());
                params.put("filepath", "/opt/data/stats_1754467453.json");
                GatherFileEncode gatherFileEncode = new GatherFileEncode();
                byte[] data = gatherFileEncode.encode(params.toString());
                tcpClient.send(data);
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
        tcpClient.connect();*/

        TcpClient tcpClient = TcpClient.getInstance();
        TcpClientWatchDog tcpClientWatchDog = new TcpClientWatchDog("127.0.0.1", 6538);
        tcpClientWatchDog.start();
      //  tcpClient.connect("172.18.1.10", 6538);
        tcpClient.connect("127.0.0.1", 6538);
    }
}
